package cm.com.carlamanda.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cm.com.carlamanda.domain.UserInfo;
import cm.com.carlamanda.service.UserService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bosch.util.Md5Util;

@Controller
@RequestMapping(value = "userAdmin")
public class UserController {
    public static Logger log = Logger.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    JSONObject jsonObject = new JSONObject();

    @RequestMapping(value = "userRegister", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addUser(@RequestBody UserInfo userInfo) {
        log.info("Method userRegister invoke!");
        // Register
        try {
            UserInfo hisUser = userService.getUser(userInfo);
            if(hisUser!=null){
                jsonObject.put("result", true);
                jsonObject.put("message","Existing userNo in the system.");
            }else{
                userService.addUser(userInfo);
                jsonObject.put("result", true);
                jsonObject.put("message","Register success.");
            }
        } catch (Exception e) {
            // if call bpm server error, we set the error info !
            log.error(e);
        }
        return jsonObject;
    }

    @RequestMapping(value = "userDelete", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject delUser(@RequestBody String userNo) {
        log.info("Method userDelete invoke!");
        // Delete
        try {
            userService.delUser(userNo);
            jsonObject.put("result", true);
        } catch (Exception e) {
            // if call bpm server error, we set the error info !
            log.error(e);
        }
        return jsonObject;
    }

    @RequestMapping(value = "userUpdate", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject editUser(@RequestBody UserInfo userInfo) {
        log.info("Method userUpdate invoke!");
        // Update
        try {
            userService.editUser(userInfo);
            jsonObject.put("result", true);
        } catch (Exception e) {
            // if call bpm server error, we set the error info !
            log.error(e);
        }
        return jsonObject;
    }

    @RequestMapping(value = "userSearch", method = RequestMethod.GET)
    @ResponseBody
    public Object searchUser() {
        log.info("Method userSearch invoke!");
        // Search\
        List<UserInfo> list = new ArrayList<UserInfo>();
        try {
            list = userService.searchUser(new UserInfo());
        } catch (Exception e) {
            // if call bpm server error, we set the error info !
            log.error(e);
        }
        return JSON.toJSON(list);
    }

    @RequestMapping(value = "userLogin", method = RequestMethod.POST)
    @ResponseBody
    public Object userLogin(@RequestBody UserInfo userInfo, HttpSession session) {
        log.info("Method userLogin invoke!");
        // set password
        String password = userInfo.getPassword();
        UserInfo loginResult = null;
        // Login
        try {
            // decode
            userInfo.setPassword(Md5Util.getMD5(password));
            // callBPMUserLoginService
            loginResult = userService.getUser(userInfo);
            if(loginResult==null){
                jsonObject.put("result", false);
            }else{
                // string2map
                jsonObject.put("result", true);
                jsonObject.put("user", loginResult);
                session.setAttribute("userGroupInfo", loginResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
            session.removeAttribute("userGroupInfo");
        }
        return jsonObject;
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "getUserSession", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getUserSession(HttpSession session) {
        log.info("Method getUserSession invoke!");
        Map<String, Object> map = new HashMap<String, Object>();
        map = (Map<String, Object>) session.getAttribute("userGroupInfo");
        map.get(map);
        boolean sessionExpired = (map == null);
        map.put("sessionExpired", sessionExpired ? true : false);
        map.put("message", sessionExpired ? "session out time,please relogin" : "succeed");
        return map;
    }

    @RequestMapping(value = "removeUserSession", method = RequestMethod.GET)
    @ResponseBody
    public void removeUserSession(HttpSession session) {
        log.info("Method removeUserSession invoke!");
        session.removeAttribute("userGroupInfo");
    }

}
